﻿using System;
using System.IO;

namespace Rail.Start.Controller
{
    public class MenuSettingCommand: Base.BaseCommand
    {
        /// <summary>
        /// 设置菜单的图标，放在Resource/Menus下，和按钮同名的png文件
        /// </summary>
        /// <param name="notification"></param>
        public override void Execute(PureMVC.Interfaces.INotification notification)
        {
            FormMain fm = notification.Body as FormMain;
            var rc = fm.RibbonControl;
            foreach(var item in rc.Items)
            {                
                if(item is DevExpress.XtraBars.BarButtonItem)
                {
                    var bbi = item as DevExpress.XtraBars.BarButtonItem;
                    var path = Commons.Paths.ApplicationResourcesDir + "\\Menus\\" + bbi.Name + ".png";
                    if (File.Exists(path))
                    {
                        bbi.ImageOptions.LargeImage = System.Drawing.Image.FromFile(path);
                    }
                    bbi.ItemClick += Bbi_ItemClick;
                }
                else if(item is DevExpress.XtraBars.BarSubItem)
                {
                    var bbi = item as DevExpress.XtraBars.BarSubItem;
                    var path = Commons.Paths.ApplicationResourcesDir + "\\Menus\\" + bbi.Name + ".png";
                    if (File.Exists(path))
                    {
                        bbi.ImageOptions.LargeImage = System.Drawing.Image.FromFile(path);
                    }
                    bbi.ItemClick += Bbi_ItemClick;
                }
            }
            Console.WriteLine("MenuSettingCommand Run");
        }
        /// <summary>
        /// 显示点击了按钮的Caption
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Bbi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SendNotification(Commons.Messages.SHOWINFOMESSAGE,"点击了菜单按钮 " +e.Item.Caption);
        }
    }
}
